package com.hellobike.haxing.modules.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.product.entity.Promotion;
import com.hellobike.haxing.modules.product.service.IPromotionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 活动主信息
 * @Author: haxing-boot
 * @Date: 2020-01-23
 * @Version: V1.0
 */
@RestController
@RequestMapping("/product/promotion")
@Slf4j
public class PromotionController extends BaseController<Promotion, IPromotionService> {
    @Autowired
    private IPromotionService promotionService;

    /**
     * 分页列表查询
     *
     * @param promotion
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "活动主信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(Promotion promotion,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<Promotion> queryWrapper = QueryGenerator.initQueryWrapper(promotion, req.getParameterMap());
        Page<Promotion> page = new Page<Promotion>(pageNo, pageSize);
        IPage<Promotion> pageList = promotionService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param promotion
     * @return
     */
    @AutoLog(value = "活动主信息-添加", operateType = 2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Promotion promotion) {
        try{
            return promotionService.savePromotion(promotion);
        }catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 编辑
     *
     * @param promotion
     * @return
     */
    @AutoLog(value = "活动主信息-编辑", operateType = 3)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Promotion promotion) {
        return promotionService.updatePromotion(promotion);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
        Promotion promotion = promotionService.getById(id);
        if (promotion == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(promotion);
    }

    @GetMapping(value = "/queryByIds")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] strs = ids.split(",");
            List<Long> idsList = new ArrayList<>();
            for (String str : strs) {
                idsList.add(Long.valueOf(str));
            }
            List<Promotion> promotionList = promotionService.list(new QueryWrapper<Promotion>()
                    .lambda().in(Promotion::getId, idsList));
            return Result.ok(promotionList);
        }
        return Result.ok("");
    }

}
